package com.sskj.b70.v.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.adapter.MyFragmentPagerAdapter;
import com.sskj.b70.common.App;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.common.rxbus2.Subscribe;
import com.sskj.b70.common.rxbus2.ThreadMode;
import com.sskj.b70.m.bean.rxbus.TabChange;
import com.sskj.b70.p.fragment.FabiFragmentPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.FabiDetailActivity;
import com.sskj.b70.v.activity.FabiPublishActivity;
import com.sskj.b70.v.activity.LoginActivity;
import com.sskj.b70.v.activity.MainActivity;
import com.sskj.b70.v.activity.SellerApplyActivity;
import com.sskj.b70.v.base.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * C2C交易
 */
public class FabiFragment extends BaseFragment<MainActivity, FabiFragmentPresenter> {

    @BindView(R.id.iv_right_end) // 右侧ImageVIew
            ImageView ivRightEnd;
    @BindView(R.id.iv_right_start)  // 左侧ImageVIew
            ImageView ivRightStart;
    @BindView(R.id.tv_buy)  // 购买
            TextView tvBuy;
    @BindView(R.id.tv_sell) // 出售
            TextView tvSell;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private AlertDialog tipDialog;
    private AlertDialog cancleTipDialog;

    public static FabiFragment newInstance() {

        Bundle args = new Bundle();

        FabiFragment fragment = new FabiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fabi;
    }

    @Override
    public FabiFragmentPresenter getPresenter() {
        return new FabiFragmentPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(FabiBuyAndSellFragment.newInstance(false));
        fragments.add(FabiBuyAndSellFragment.newInstance(true));
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                changeTextColor(position);
            }
        });
        ClickUtil.click(tvBuy, () -> {
            changeTab(new TabChange(true));
        });
        ClickUtil.click(tvSell, () -> {
            changeTab(new TabChange(false));
        });
        ClickUtil.click(ivRightStart, () -> {
            if (MyAppLication.isLogin()) {
                FabiDetailActivity.start(getActivity());
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiFragment0));
                LoginActivity.start(getActivity());
            }
        });
        ClickUtil.click(ivRightEnd, () -> {//发布单子
            if (!MyAppLication.isLogin()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiFragment0));
                LoginActivity.start(getActivity());
                return;
            }
            if (!checkIdent()) {
                return;
            }
//            if (!mActivity.checkPwdStatus()) {
//                return;
//            }
            if (!MyAppLication.isShop()) {
                showTip();
                return;
            }
            FabiPublishActivity.start(mActivity);

        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeTab(TabChange tabChange) {
        int i = tabChange.isBuy() ? 1 : 0;
        changeTextColor(i);
        if (viewPager.getCurrentItem() != i) {
            viewPager.setCurrentItem(i, true);
        }
    }

    /**
     * 更改选项卡
     *
     * @param position
     */
    private void changeTextColor(int position) {
        tvBuy.setTextColor(getResources().getColor(position == 1 ? R.color.bull_black : R.color.bull_black_no_select));
        tvSell.setTextColor(getResources().getColor(position == 0 ? R.color.bull_black : R.color.bull_black_no_select));
    }

    /**
     * 显示申请商家的提示
     */
    private void showTip() {
        if (tipDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            View view = View.inflate(getActivity(), R.layout.dialog_fabi_tip, null);
            ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
                tipDialog.dismiss();
            });
            ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
                tipDialog.dismiss();
                SellerApplyActivity.start(getActivity());
            });
            builder.setView(view);
            builder.setCancelable(true);
            tipDialog = builder.create();
        }

        tipDialog.show();
    }

    /**
     * 显示解除商家认证
     */
    private void showCancleTip() {
        if (cancleTipDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            View view = View.inflate(getActivity(), R.layout.dialog_fabi_cancle_seller_tip, null);
            ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
                cancleTipDialog.dismiss();
            });
            ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
                cancleTipDialog.dismiss();
                SellerApplyActivity.start(getActivity());
            });
            builder.setView(view);
            builder.setCancelable(true);
            cancleTipDialog = builder.create();
        }

        cancleTipDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

}
